package vlt.el.main;

import java.util.List;
import java.util.Map;

import vlt.el.db.DBConnection;
import vlt.el.db.SQLGenerator;
import vlt.el.db.dao.VltDAO;
import vlt.el.util.Config;

public class InitialLoadT implements Runnable {

	private int thread;
	private String initialId;
	private String finalId;

	public int getThread(){
		return this.getThread();
	}
	
	public void setThread(int thread){
		this.thread = thread;
	}
	
	public String getInitialId() {
		return initialId;
	}

	public void setInitialId(String initialId) {
		this.initialId = initialId;
	}

	public String getFinalId() {
		return finalId;
	}

	public void setFinalId(String finalId) {
		this.finalId = finalId;
	}

	@Override
	public void run() {
		System.out.println("starting thread " + thread + ", rowids " + this.initialId + " to " + this.finalId);
		
//		if (1==1){
//			try {
//				Thread.sleep(5*1000);
//			} catch (InterruptedException e) {
//			}
//			System.out.println("executing thread rowids " + this.initialId + " to " + this.finalId);
//			return;
//		}
		
		DBConnection db = new DBConnection();
		try {

			Config config = Config.getInstance();
			config.reloadConfigProperties();
		
			db.connectOrig(true);
			db.connectDest(true);
			SQLGenerator sqlGenerator = new SQLGenerator();
			
			//String selectOrig = sqlGenerator.generateSelect(
			//		config.getConfigProperty("database.orig.owner"),
			//		config.getConfigProperty("database.orig.table"),
			//		initialId,
			//		finalId );
			
			Map<String, String> columns = sqlGenerator.listTableColumns(
					config.getConfigProperty("database.orig.owner"),
					config.getConfigProperty("database.orig.table"),
					db.getConnOrig());
			String insertDest = sqlGenerator.generateInsert(
					config.getConfigProperty("database.dest.owner"),
					config.getConfigProperty("database.dest.table"), columns);
			
			int rowsBeforeCommit = Integer.parseInt(config	.getConfigProperty("database.rows_before_commit"));
			int rowsSelect = Integer.parseInt(config	.getConfigProperty("database.rows_select"));
			String owner = config.getConfigProperty("database.orig.owner");
			String table = config.getConfigProperty("database.orig.table");


			VltDAO dao = new VltDAO();
			List<String[]> idsList = sqlGenerator.splitTableById(owner, table, db.getConnOrig(), rowsSelect, initialId, finalId);
			System.out.println("   Thread: " + thread + " - idsList size: " + idsList.size() + ", split table: " + initialId + " to " + finalId);
			for (String[] ids : idsList){
				String selectOrig = sqlGenerator.generateSelect(
						config.getConfigProperty("database.orig.owner"),
						config.getConfigProperty("database.orig.table"),
						ids[0],
						ids[1] );
				System.out.println("      Thread: " + thread + " load from: " + selectOrig);
				dao.load(db.getConnOrig(), db.getConnDest(), selectOrig, insertDest, columns, rowsBeforeCommit, thread);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				db.disconnectOrig();
				db.disconnectDest();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
